package util;

/*
 *@author lim �������� 2007-4-20
 *
 *E-mail: cliffle007@163.com 
 */
import java.util.Date;
import java.util.Calendar;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Timehelper {
	public static String getCurTime() {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf.format(new Date());

		return nowTime;
	}

	public static String getDateStr(Date time) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss.sss");
		String nowTime = sdf.format(time);

		return nowTime;
	}
	public static String getDate(Date time) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String nowTime = sdf.format(time);

		return nowTime;
	}

	public static String getCurDate() {
		// java.text.SimpleDateFormat sdf=new
		// java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String nowTime=sdf.format(new Date());

		// return nowTime.substring(1, 9);
		// +nowTime.substring(5,6)+nowTime.substring(7,8);
		String time = getCurTime();
		// System.out.println(time);
		return getYear(time) + getMon(time) + getDate(time);
	}
	public static String getCurDate(String sep) {
		// java.text.SimpleDateFormat sdf=new
		// java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String nowTime=sdf.format(new Date());

		// return nowTime.substring(1, 9);
		// +nowTime.substring(5,6)+nowTime.substring(7,8);
		String time = getCurTime();
		// System.out.println(time);
		return getYear(time) +sep+ getMon(time) +sep+getDate(time);
	}
	public static String getYesterday(String sep){
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
		"yyyy-MM-dd HH:mm:ss");
		String yes = sdf.format(new Date(System.currentTimeMillis()-24*60*60*1000));
		return getYear(yes) +sep+ getMon(yes) +sep+ getDate(yes);
	}

	public static String get20secTime() {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date d1 = new Date();

		String nowTime = sdf.format(d1);
		d1.setSeconds((new Integer(getSec(nowTime)).intValue() / 20) * 20);

		nowTime = sdf.format(d1);
		return nowTime;
	}

	public static String getYear(String timeStr) {
		Calendar cal = dateString2Calendar(timeStr);

		String year = new Integer(cal.get(Calendar.YEAR)).toString();
		return year;
	}

	public static String getMon(String timeStr) {
		// System.out.println(timeStr);
		Calendar cal = dateString2Calendar(timeStr);
		String Mon;
		// System.out.println("month"+Calendar.MONTH);
		Mon = new Integer(cal.get(Calendar.MONTH) + 1).toString();

		/*
		 * if (Calendar.MONTH<9) Mon="0"+new
		 * Integer(cal.get(Calendar.MONTH)+1).toString(); else if
		 * (Calendar.MONTH>9) Mon=new
		 * Integer(cal.get(Calendar.MONTH)+1).toString();
		 */
		if (Mon.length() == 1)
			Mon = "0" + Mon;
		return Mon;
	}

	public static String getDate(String timeStr) {
		Calendar cal = dateString2Calendar(timeStr);
		String Date = new Integer(cal.get(Calendar.DATE)).toString();
		if (Date.length() == 1)
			Date = "0" + Date;
		return Date;
	}

	public static String gethour(String timeStr) {
		Calendar cal = dateString2Calendar(timeStr);
		String Hour = new Integer(cal.get(Calendar.HOUR_OF_DAY)).toString();
		return Hour;
	}

	public static String getMin(String timeStr) {
		Calendar cal = dateString2Calendar(timeStr);
		String Min = new Integer(cal.get(Calendar.MINUTE)).toString();
		return Min;
	}

	public static String getSec(String timeStr) {
		Calendar cal = dateString2Calendar(timeStr);
		String Sec = new Integer(cal.get(Calendar.SECOND)).toString();
		return Sec;
	}

	public static Calendar dateString2Calendar(String s) {
		// SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.SIMPLIFIED_CHINESE);
		Calendar cal = Calendar.getInstance();

		try {

			// System.out.println("s:"+s);
			Date d1 = df.parse(s);
			// System.out.println(new Date().toString());
			// Date d1=df.parse(new Date().toString());
			// System.out.println(d1.toString());
			// d1=df.parse(s, Locale.SIMPLIFIED_CHINESE);
			// Date d1=new Date().parse(s);
			// System.out.println(d1.toString());
			cal.setTime(d1);
			// cal.setTime(new Date());

		} catch (Exception e) {
			System.out.println("dateString2Calendar error");
		}

		return cal;
	}

	public static Date str2Date(String s) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.SIMPLIFIED_CHINESE);
		Date d1 = null;
		try {

			d1 = df.parse(s);

		} catch (Exception e) {
			System.out.println("str2Date error");
		}

		return d1;
	}

	public static String getTimeStr(int year, int month, int day, int hour,
			int min, int sec) {
		String time = null;
		try {
			Date d1 = new Date();
			d1.setYear(year);
			d1.setMonth(month);
			d1.setDate(day);
			d1.setHours(hour);
			d1.setMinutes(min);
			d1.setSeconds(sec);
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			time = sdf.format(d1);
		} catch (Exception e) {
			System.out.println("getTimeStr error");
		}
		return time;

	}

	public static float getFloatValue(int lo, int hi) {
		int value = (hi << 16) | lo;
		return Float.intBitsToFloat(value);
	}
	public static byte[] floatToByte(float v) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        byte[] ret = new byte [4];
        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(v);
        bb.get(ret);
        return ret;
    }

	public static void main(String[] args)
	{
		System.out.println(Timehelper.getDate(new Date()));
		//System.out.print(getTimeStr(2002, 1, 1, 1, 1, 1));
		Date date= new Date();
		date.setTime(0);
		date.setTime(1332230403000L+8*60*60*1000);
		System.out.println(date.toGMTString());
		
		System.out.println(System.currentTimeMillis());
		
		System.out.println(Timehelper.getFloatValue(Integer.parseInt("0FDA", 16), Integer.parseInt("4049", 16)));
		System.out.println(Timehelper.getFloatValue(Integer.parseInt("5094", 16), Integer.parseInt("4743", 16)));
		
		System.out.println(Timehelper.getFloatValue(Integer.parseInt("74fa", 16), Integer.parseInt("b793", 16)));
		
		System.out.println(Timehelper.getFloatValue(Integer.parseInt("147a", 16), Integer.parseInt("41bc", 16)));
		
		System.out.println(Timehelper.getFloatValue(Integer.parseInt("f5c3", 16), Integer.parseInt("4048", 16)));
		byte[] bts = floatToByte(0F);
		System.out.println(DataUtil.BytesToHexStr(bts));
		//Float f= new Float(3.14);

	
		//System.out.println(f.toString());
		
		//E0329450
	}
}
